<?php
//注册命名空间.
\think\Loader::addNamespace('service', '../service/');
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function dateof($data){
    if($data==false)return '';
    return date('Y-m-d H:i:s',$data);
}

function rpc_request($url, $param, $method = "post")
{
    //设置选项
    $opts = array(
        CURLOPT_TIMEOUT => 60,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_URL => $url,
        //CURLOPT_USERAGENT      => $_SERVER['HTTP_USER_AGENT'],
    );
    if ($method === 'post') {
        $opts[CURLOPT_POST] = 1;
        $opts[CURLOPT_POSTFIELDS] = $param;
    }

    //初始化并执行curl请求
    $t1 = microtime(true);

    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    $data = curl_exec($ch);
    $curlResult = curl_getinfo($ch);

    $error = curl_error($ch);
    curl_close($ch);

    $t2 = microtime(true);

    $runTime = '  耗时' . ($t2 - $t1) . '秒  ';


    $debugFileName = RUNTIME_PATH . DS . "log" . DS . "debug_" . date("Y-m-d") . ".txt";

    if (is_array($curlResult)) {
        if ($curlResult["total_time"] > 0.8) {
            error_log("有点慢，超过了0.8秒:\r\n", 3, $debugFileName);
        }
    }
    $log = "请求的url:" . $url . " 请求的参数：" . json_encode($param) . "  请求时间" . date("Y-m-d H:i:s") . $runTime;

    error_log($log, 3, $debugFileName);
    error_log("\r\n\r\n", 3, $debugFileName);
    error_log(print_r($curlResult, true), 3, $debugFileName);
    error_log("\r\n\r\n\r\n\r\n\r\n\r\n", 3, $debugFileName);


    if ($ret_json = true) {
        $return_data = json_decode($data, true);
    }
    return $return_data;
}